package org.example.cake.service.impl;

import org.example.cake.entity.OrderDetail;
import org.example.cake.mapper.OrderDetailMapper;
import org.example.cake.service.OrderDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:订单详情业务层实现
 * @Author: luojiangjin
 * @Date: 2025/8/9 12:07
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Override
    public OrderDetail getOrderDetailById(Integer orderId) {
        OrderDetail orderDetail = orderDetailMapper.selectOrderDetailById(orderId);
        return orderDetail;
    }

    @Override
    public Map<String, Object> getOrderWithCakesById(Integer orderId) {
        // 1. 查询订单基础信息（订单号、用户ID、地址等）
        OrderDetail orderBase = orderDetailMapper.selectOrderBaseById(orderId);
        if (orderBase == null) {
            return null;
        }

        // 2. 查询该订单包含的所有蛋糕信息（多条记录）
        List<Map<String, Object>> cakesList = orderDetailMapper.selectCakesByOrderId(orderId);

        // 3. 组装数据：订单基础信息 + 蛋糕列表
        Map<String, Object> result = new HashMap<>();
        // 订单基础字段
        result.put("orderid", orderBase.getOrderId());
        result.put("userid", orderBase.getUserId());
        result.put("ordernum", orderBase.getOrderNum());
        result.put("ordertime", orderBase.getOrderTime());
        result.put("orderstatus", orderBase.getOrderStatus());
        result.put("refund", orderBase.getRefundStatus()); // 退款状态
        result.put("orderecount", orderBase.getOrderCount()); // 商品总数量
        result.put("orderprice", orderBase.getOrderPrice()); // 订单总价格
        result.put("adddate", orderBase.getAdddate()); // 订单总价格
        // 配送与支付信息（新增）
        result.put("deliverypay", orderBase.getDeliveryFee()); // 配送费
        result.put("remark", orderBase.getRemark()); // 订单备注

        // 收货信息（新增）
        result.put("consignee", orderBase.getConsignee()); // 收货人
        result.put("phone", orderBase.getPhone()); // 手机号
        result.put("province", orderBase.getProvince()); // 省份
        result.put("city", orderBase.getCity()); // 城市
        result.put("district", orderBase.getDistrict()); // 区/县
        result.put("address", orderBase.getDetailAddress()); // 详细地址

        // 优惠券信息（新增）
        result.put("discountname", orderBase.getDiscountName()); // 优惠券名称
        result.put("maxmoney", orderBase.getMaxMoney()); // 优惠券最大金额
        result.put("money", orderBase.getDiscountMoney()); // 优惠金额

        // 蛋糕列表
        result.put("cakesList", cakesList);

        return result;
    }
}
